<?php
# 文件名称:usergroup.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
defined( '_SYS' ) or die( 'No direct script access allowed' );
class C_tableUsergroup extends C_table
{
	var $id;
	var $parent_id;
	var $lft;
	var $rgt;
	var $title;
	var $section_id = '11';
	var $section = 'backend';

	function C_tableUsergroup(&$db)
	{
		parent::__construct($db->_table_prefix.'usergroups', 'id', $db);
	}
	/**
	 * Method to check the current record to save
	 *
	 */
	function check()
	{
		// Validate the title.
		if ((trim($this->title)) == '') {
			return false;
		}
		// Validate the section.
		if (empty($this->section_id)) {
			return false;
		}
		return true;
	}
	/**
	 * 重塑权限树
	 */
	function rebuild($parent_id = 0, $left = 0)
	{
		// get the database object
		$db = &$this->_db;
		// get all children of this node
		$db->setQuery(
			'SELECT id FROM '. $this->_tb_name .
			' WHERE parent_id='. (int)$parent_id .
			' ORDER BY parent_id, title'
		);
		$children = $db->loadResultArray();
		
		// the right value of this node is the left value + 1
		$right = $left + 1;
		// execute this function recursively over all children
		for ($i=0,$n=count($children); $i < $n; $i++)
		{
			// $right is the current right value, which is incremented on recursion return
			$right = $this->rebuild($children[$i], $right);
			// if there is an update failure, return false to break out of the recursion
			if ($right === false) {
				return false;
			}
		}
		// we've got the left value, and now that we've processed
		// the children of this node we also know the right value
		$db->setQuery(
			'UPDATE '. $this->_tb_name .
			' SET lft='. (int)$left .', rgt='. (int)$right .
			' WHERE id='. (int)$parent_id
		);
		// if there is an update failure, return false to break out of the recursion
		if (!$db->query()) {
			return false;
		}
		// return the right value of this node + 1
		return $right + 1;
	}
	function store($updateNulls = false)
	{
		if ($result = parent::store($updateNulls))
		{			
			// Rebuild the nested set tree.
			$this->rebuild();
		}
		return $result;
	}
	/**
	 * Delete this object and it's dependancies
	 */
	function delete($oid = null)
	{
		$k = $this->_tb_key;
		if ($oid) {
			$this->load($oid);
		}
		if ($this->id == 0) {
			return false;
		}
		if ($this->parent_id == 0) {
			return false;
		}
		if ($this->lft == 0 or $this->rgt == 0) {
			return false;
		}
		$db = &$this->getDbo();
		// Select the category ID and it's children
		$db->setQuery(
			'SELECT c.id' .
			' FROM `'.$this->_tb_name.'` AS c' .
			' WHERE c.lft >= '.(int) $this->lft.' AND c.rgt <= '.$this->rgt
		);
		$ids = $db->loadResultArray();
		if (empty($ids)) {
			return false;
		}
		$ids = implode(',', $ids);
		// Delete the category dependancies
		// Delete the category and it's children
		$db->setQuery(
			'DELETE FROM `'.$this->_tb_name.'`' .
			' WHERE id IN ('.$ids.')'
		);
		if (!$db->query()) {
			return false;
		}
		// Delete the user to usergroup mappings for the group(s) from the database.
		$db->setQuery(
			'DELETE FROM `'.$this->_db->_table_prefix.'user_usergroup_map`' .
			' WHERE `group_id` IN ('.$ids.')'
		);
		$db->query();
		
		// Delete the usergroup to rule mappings for the group(s) from the database.
		$db->setQuery(
			'DELETE FROM `'.$this->_db->_table_prefix.'usergroup_rule_map`' .
			' WHERE `group_id` IN ('.$ids.')'
		);
		$db->query();
		// Check for a database error.
		if ($db->getErrorNum()) {
			return false;
		}
		return true;
	}
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>